package kelisPkg.loadPackage
{
	
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	public class MultiMaterialsManager extends EventDispatcher
	{
		public function MultiMaterialsManager(target:IEventDispatcher=null)
		{
			super(target);
		}
		static private var _instance:MultiMaterialsManager;
		static public function get instance():MultiMaterialsManager{
			_instance=_instance||new MultiMaterialsManager;
			return _instance;
		}
		
		private var _vecId:int;
		private var _thisUrlVec:Vector.<String>;
		public function addUrlVec(vec:Vector.<String>):void
		{
			_vecId=0;
			_thisUrlVec=vec;
			if(vec.length<1){
				dispatchEvent(new LoadEvent(LoadEvent.LOAD_COMPLETE,_thisUrlVec));
				return
			}
			MaterialsManager.instance.addEventListener(LoadEvent.LOAD_COMPLETE,_loadComplete);
			MaterialsManager.instance.addEventListener(LoadEvent.LOAD_PROGRESS,_loadProgress)
			_loadById();
		}
		private function _loadById(id:int=0):void
		{
			MaterialsManager.instance.addUI(_thisUrlVec[_vecId]);
			_vecId+=1;
			
		}
		private function _loadProgress(e:LoadEvent):void
		{
			var ix:Number=e.info as Number;
			var iy:Number=100*(_vecId-1+(ix/100))/_thisUrlVec.length;
			dispatchEvent(new LoadEvent(LoadEvent.LOAD_PROGRESS,iy));
		}
		private function _loadComplete(e:LoadEvent):void
		{
			if(_vecId==_thisUrlVec.length){
				dispatchEvent(new LoadEvent(LoadEvent.LOAD_COMPLETE,_thisUrlVec));
			}else{
				_loadById(_vecId);
			}
		}
	}
}